#pragma once

#include <services/scheduler.hpp>
#include <services/provider.hpp>

#include <QtGui/QMainWindow>
#include <engine.core/qt_hash.hpp>

#include "GeneratedFiles\ui\ui_mainwindow.h"
#include "Document.hpp"

#include "GlyphModel.hpp"
#include "FontModel.hpp"
#include "ErrorModel.hpp"

#include "GlyphWidget.hpp"
#include "CssHighlighter.hpp"




class MainWindow
	: public QMainWindow
{
	Q_OBJECT

public:

	MainWindow(QWidget *parent = 0);
	~MainWindow();



	/**
	 * Sets the given document as the new visible document of this window.
	 * All views will be updated, the menus, etc...
	 */
	void setDocument(const Document::ptr& document);

	/**
	 * Sets the given title as a part of the new window title.
	 */
	void setWindowTitle(QString fileName);

	/**
	 * Sets the state of the current active document to 'changed'.
	 * Updates actions (enabled/disabled), the window title, etc...
	 */
	void setDocumentChanged(bool changed);

	/**
	 * Sets the given EditWidget as the new visible EditWidget of this window.
	 * Will pop up a dialog if the current edit widget's data is unsaved.
	 * Ownership is transfered to the callee.
	 */
	void setEditWidget(EditWidget* widget);

public slots:

	void onNew();
	void onOpen();
	void onSave();
	void onSaveAs();
	void onExit();
	void onAddGlyph();
	void onAddFont();

	void onCssDocumentChanged();

	void onGlyphClicked(const QModelIndex& index);
	void onFontClicked(const QModelIndex& index);
	void onRemove();

	void onEditChanged();
	void onEditApply();
	void onEditDiscard();

	void onSelectionChanged();

	void onAbout();
	void onAboutQt();

	void onUpdate();

protected:

	/**
	 * This is the entry point for the parsing thread
	 * that regularly parses the code from the main document.
	 */
	void parsing_thread();

	/**
	 * 
	 */
	void closeEvent(QCloseEvent* event);

	/**
	 * Tests if the current document has been modified and pops up the "Save changes" dialog.
	 * Actually saves the document (when the user wants to).
	 *
	 * @return true when the application should close
	 * @return false when it should not
	 */
	bool shouldClose();

	/**
	 * Saves all application & window related settings in a QSettings object
	 * to be restored another time.
	 */
	void writeSettings();

	/**
	 * Restores all application & window related settings from a QSettings object.
	 */
	void restoreSettings();

private:

	QSettings                  m_settings;

	Ui::MainWindow             ui;

	Document::ptr              m_document;

	services::scheduler::ptr   m_scheduler;
	services::future           m_parsedDocument;

	// Both models visualize the document through their view
	GlyphModel                 m_glyphModel;
	FontModel                  m_fontModel;
	ErrorModel                 m_errorModel;

	bool                       m_changedSinceParsing;

	CssHighlighter             m_syntaxHighlighter;
	QString                    m_cssContent;
	EditWidget*                m_editWidget;

	QTimer                     m_timer;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
